package com.hcss.common.util.excel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Author: 可乐
 * @Description:
 * @CreateTime: 2023-05-26  10:59
 */
public class ImageUtils {


		public static double getWhiteRatio(File file) throws IOException {
			BufferedImage image = ImageIO.read(file);
			int width = image.getWidth();
			int height = image.getHeight();
			int[] pixels = new int[width * height];
			image.getRGB(0, 0, width, height, pixels, 0, width);

			int whiteCount = 0;
			for (int i = 0; i < pixels.length; i++) {
				int pixel = pixels[i];
				int r = (pixel >> 16) & 0xff;
				int g = (pixel >> 8) & 0xff;
				int b = pixel & 0xff;
				// 判断是否为白色像素
				if (r > 200 && g > 200 && b > 200) {
					whiteCount++;
				}
			}

			double whiteRatio = (double) whiteCount / (width * height)*100;
			return whiteRatio;
		}



	public static void main( String[] args ) throws IOException {
		double whiteRatio=getWhiteRatio( new File( "E:\\result_pictures\\upload3-R-ab836c8309e40e.jpg" ) );
		String s=String.valueOf( whiteRatio ).substring( 0,5 );
		System.out.println(s.substring( 0,5 ) );

	}

}